package com.dfs.parent.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfs.parent.common.core.domain.Result;
import com.dfs.parent.order.dto.OrderDTO;
import com.dfs.parent.order.entity.Order;
import com.dfs.parent.order.mapper.OrderMapper;
import com.dfs.parent.order.service.OrderService;
import com.dfs.parent.service.vo.ProductVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    private final OrderMapper orderMapper;
    private final ApplicationContext applicationContext;



    /**
     * 创建订单
     */
    @PostMapping("/create")
    public Result<ProductVO> create(@RequestBody OrderDTO order, HttpServletRequest request) throws Exception {
        Object obj = request;
       // int a =10/0;
        System.out.println(request);
        System.out.println("order start===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ProductVO productVO = orderService.createOrder(order,request);
        System.out.println("order end===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return Result.ok(productVO);
//        return Result.ok();
    }
    @GetMapping("/orderConfig")
    public Result<String> orderConfig() {
        //  order.setId(Kit.creatId());

        Result<String> config = orderService.orderConfig();
        return config;
    }
    @GetMapping("/orderPort")
    public Result<String> orderPort() {
        //  order.setId(Kit.creatId());

        String port =  applicationContext.getEnvironment().getProperty("server.port");

        return Result.ok(port);
    }


    @GetMapping("/create1")
    public Result create(Long id ) throws Exception {

        return Result.ok(orderMapper.deleteById(id));
//        return Result.ok();
    }
    @GetMapping("/findById")
    public Result<Order> get(Long id ) throws Exception {
        return Result.ok(orderService.findById(id));
//        return Result.ok();
    }
    @GetMapping("/findById1")
    public Result<Order> get1(Long id,Long userId ) throws Exception {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getId,id).eq(Order::getUserId,userId);
        return Result.ok(orderMapper.selectOne(queryWrapper));
//        return Result.ok();
    }

//    @GetMapping("/aa")
//    public Result<ProductVO> get() {
//        //  order.setId(Kit.creatId());
//        OrderDTO order = new OrderDTO();
//        order.setCount(100);
//        ProductVO productVO = orderService.createOrder(order);
//        return Result.ok(productVO);
//    }
//    /**
//     * <h2>初始化流控规则</h2>
//     * */
//    @PostConstruct
//    public void init() {
//
//        // 流控规则集合
//        List<FlowRule> flowRules = new ArrayList<>();
//        // 创建流控规则
//        FlowRule flowRule = new FlowRule();
//        // 设置流控规则 QPS, 限流阈值类型 (QPS, 并发线程数)
//        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        // 流量控制手段
//        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
//        // 设置受保护的资源
//        flowRule.setResource("flowRuleCodehh");
//        // 设置受保护的资源的阈值
//        flowRule.setCount(1);
//        flowRules.add(flowRule);
//
//        // 加载配置好的规则
//        FlowRuleManager.loadRules(flowRules);
//    }
//    @GetMapping("/flow-rule")
//    @SentinelResource(value = "flowRuleCodehh")
//    public Result<String> flowRuleCode() {
//        log.info("request flowRuleCode");
//        return  Result.ok( "imooc-qinyi-ecommerce");
//    }
}
